package com.example.controller;

import org.elasticsearch.action.ingest.PutPipelineRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * 导入数据联系
 *
 * @author 夏智峰
 * @create 2018-03-14 15:29
 */
public class ImportSourceController {
  @Autowired
  private TransportClient client;

  public ResponseEntity importMySQL(){
    try {
      BytesReference source = jsonBuilder()
              .startObject()
              .field("description", "my_pipeline")
              .startArray("processors")
              .startObject().startObject("lowercase").endObject().endObject()
              .endArray()
              .endObject()
              .bytes();

      PutPipelineRequest request = new PutPipelineRequest("1", source, XContentType.JSON);
      client.admin().cluster().putPipeline(request).get();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      e.printStackTrace();
    }

    return new ResponseEntity(HttpStatus.OK);
  }
}
